package com.FH.applyFrom.contract;

import com.FH.applyForm.officeServies.contract_type.ContractTypePullDTO;
import com.FH.applyForm.officeServies.contract_type.ContractTypeResource;
import com.FH.applyForm.officeServies.contract_type.ContractTypeShowDTO;
import com.FH.applyForm.officeServies.contractsecond_type.ContractSecondResource;
import com.FH.applyForm.officeServies.meeting.MeetingResource;
import com.FH.organization.Attachment.AttachmentResource;
import com.FH.organization.department.DepartmentResource;
import com.FH.organization.operator_log.OperatorLogPullDTO;
import com.FH.organization.operator_log.OperatorLogResource;
import com.FH.organization.remind.RemindPullDTO;
import com.FH.organization.remind.RemindResource;
import com.FH.organization.suggestion.SuggestionResource;
import com.FH.organization.work_flow.WorkFlowPullDTO;
import com.FH.organization.work_flow.WorkFlowResource;
import com.FH.util.AccessDB;
import com.FH.util.FileProcess;
import com.FH.util.ResultDTO;
import com.FH.util.warnSrc;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import javax.ejb.Stateless;
import javax.inject.Inject;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by acer on 2016/7/15.
 */
@Stateless
public class ContractDAOImpl extends AccessDB implements ContractDAO {

    /**
     * 获取合同信息
     *
     * @param search
     * @param page
     * @param size
     * @return
     */

    @Inject
    OperatorLogResource operatorLogResource;
    @Inject
    WorkFlowResource workFlowResource;
    @Inject
    ContractResource contractResource;
    @Inject
    ContractTypeResource contractTypeResource;
    @Inject
    AttachmentResource attachmentResource;
    @Inject
    DepartmentResource departmentResource;
    @Inject
    MeetingResource meetingResource;
    @Inject
    ContractSecondResource contractSecondResource;
    @Inject
    RemindResource remindResource;
    @Inject
    SuggestionResource suggestionResource;


    public ResultDTO getContractInfo(String search, String page, String size) {

        ResultDTO resultDTO = new ResultDTO();
        try {
            JSONObject json = new JSONObject(search);
            String existName = "0";

            String sqlexit = "select count(1) from contract where 1=1 ";

            String sql_w = "";//查询条件sql组合

            String sql_page = "";//分页sql组合
            Integer page_ok = Integer.parseInt(page);
            Integer size_ok = Integer.parseInt(size);
            if (page.equals("1")) {
                sql_page = " limit 0," + size + "";
            } else {
                sql_page = " limit " + ((page_ok - 1) * size_ok) + "," + size + "";
            }

            if (json.has("isverify")) {
                sql_w += " AND isverify ='" + json.getString("isverify") + "'";
            }
//            else {
//                sql_w += " AND (isverify IS NULL OR isverify=0)";
//            }


            if (json.has("contract_id")) {
                sql_w += " AND contract_id = '" + json.getString("contract_id") + "'";
            }
            if (json.has("member_id")) {
                sql_w += " AND member_id = '" + json.getString("member_id") + "'";
            }
            String sql_ok = sqlexit + sql_w;//组合好的sql语句
            existName = getReportingToOne(sql_ok);

            List<String[]> seresult = new ArrayList<String[]>();

            List<ContractShowDTO> result = new ArrayList<ContractShowDTO>();
            if (!existName.equals("0")) {
                String sql = "select contract_id,contract_firstmember,contract_secondmember,contract_code,contract_type ,member_id,contract_signdate,contract_takedate,attachment_id,isverify,contract_enddate,contract_desc,contract_updatetime,fail_resource  from contract  where 1=1 " + sql_w + " ORDER BY create_time DESC " + sql_page;
                seresult = this.getReportingTo(sql);

                //构建返回对象
                for (int i = 0; i < seresult.size(); i++) {

                    ContractShowDTO contract = new ContractShowDTO();
                    contract.setContract_id(seresult.get(i)[1]);
                    contract.setContract_firstmember(seresult.get(i)[2]);
                    contract.setContract_secondmember(seresult.get(i)[3]);
                    contract.setContract_code(seresult.get(i)[4]);
                    contract.setContract_type(seresult.get(i)[5]);
                    contract.setMember_id(seresult.get(i)[6]);
                    contract.setContract_signdate(seresult.get(i)[7]);
                    contract.setContract_takedate(seresult.get(i)[8]);

                    contract.setAttachment_id(seresult.get(i)[9]);
                    contract.setIsverify(seresult.get(i)[10]);


                    contract.setContract_enddate(seresult.get(i)[11]);
                    contract.setContract_reason(seresult.get(i)[12]);
                    contract.setContract_updatetime(seresult.get(i)[13]);
                    contract.setFail_resource(seresult.get(i)[14]);

                    ResultDTO worklist = null, userlist = null, attachment = null, suggestion=null,organization = null,contact_type=null;
                    if (json.has("isverify")) {
                        String state = json.getString("isverify");
                        worklist = workFlowResource.getWorkFlowInfo("{work_flow_formid:" + seresult.get(i)[1] + ",work_flow_state:" + state + "}", "1", "20");
                    } else {
                        worklist = workFlowResource.getWorkFlowInfo("{work_flow_formid:" + seresult.get(i)[1] + "}", "1", "20");
                    }

                    contract.setWs(worklist.getValues());

                    //合同类型信息
                    contact_type=contractSecondResource.getContractSecondInfo("{contractsecond_id:" + seresult.get(i)[5] + "}", "1", "20");
                    contract.setContact_type(contact_type.getValues());
                    /**
                     * 提醒人员
                     */
                    userlist = contractTypeResource.getContractTypeInfo("{contract_id:" + seresult.get(i)[1] + "}", "1", "20");
                    contract.setWarnperople(userlist.getValues());

                    /**
                     * 附件集合
                     */
                    attachment = attachmentResource.getAttachmentInfo("{attachment_id:" + seresult.get(i)[9] + "}", "1", "20");
                    contract.setAttachment(attachment.getValues());

//                    /**
//                     * 部门名称
//                     */
//                    organization = departmentResource.getDepartmentInfo("{organzation_id:" + seresult.get(i)[4] + "}", "1", "20");
//                    contract.setAa(organization.getValues());
                    if(seresult.get(i)[1] != null && ! seresult.get(i)[1].equals("")){

                        suggestion=suggestionResource.getSuggestionInfo("{form_id:" + seresult.get(i)[1] + "}", "1", "100");
                        contract.setSuggestionShowDTOs(suggestion.getValues());
                    }


                    result.add(contract);
                }
            }
            if (seresult.size() != 0) {
                resultDTO.setStatus("90000");
                resultDTO.setDescritpion(warnSrc.get_success);
            } else {
                resultDTO.setStatus("90001");
                resultDTO.setDescritpion(warnSrc.get_fail);
            }
            resultDTO.setStr(result.size() + "");
            resultDTO.setValues(result);


        } catch (JSONException e) {
            e.printStackTrace();
        }
        return resultDTO;
    }

    //连表查询（部门表）
    @Override
    public ResultDTO getContractsInfo(String contract_id, String page, String size) {
//        ResultDTO resultDTO = new ResultDTO();
//
//        try {
//            JSONObject json = new JSONObject(contract_id);
//            String existName = "0";
//
//            //先查询数据库中是否存在字段
//            String sqlexit = "select count(2) from contract  where 1=1 ";
//
//            String sql_w = "";//查询条件sql组合
//            String sql_page = "";//分页组合
//
//            Integer page_ok = Integer.parseInt(page);//将定义page、size的String类型转换成Integer类型
//            Integer size_ok = Integer.parseInt(size);
//
//            if (page.equals("1")) {
//                sql_page = " limit 0," + size + "";
//            } else {
//                sql_page = " limit " + ((page_ok - 1) * size_ok) + "," + size + "";
//            }
//
//            if (json.has("member_id")) {
//                sql_w += " AND member_id ='" + json.getString("member_id") + "'";
//            }
//            if (json.has("finance_budget_title")) {
//                sql_w += " AND finance_budget_title like '%" + json.getString("finance_budget_title") + "%'";
//            }
//            if (json.has("contract_id")) {
//                sql_w += " AND contract_id ='" + json.getString("contract_id") + "'";
//            }
//
//            if(json.has("isverify")){
//                sql_w += " AND isverify ='" + json.getString("isverify")+"'";
//            }else{
//                sql_w += " AND (isverify IS NULL OR isverify=0)";
//            }
//
//
//            String sql_ok = sqlexit + sql_w;//组合好的sql语句
//            existName = getReportingToOne(sql_ok);
//
//            List<String[]> seresult = new ArrayList<String[]>();
//
//            List<ContractShowDTO> result = new ArrayList<ContractShowDTO>();
//
//            if (!existName.equals("0")) {
//                String sql = " select select contract_id,contract_firstmember,contract_code,contract_secondmember ,organzation_id contract_type ,member_id,contract_signdate,contract_takedate,attachment_id,isverify,contract_enddate,contract_reason  ,r.organization_name,a.attachment_addr FROM contract AS f,organization AS r ,attachment a  where f.organization_id = r.organization_id and  f.attachment_id=a.attachment_id " + sql_w +" ORDER BY contract_date DESC "+ sql_page;
//                //     String sql ="select * from finance_budget" ;
//                seresult = this.getReportingTo(sql);
//
//                //构建返回对象
//                for (int i = 0; i < seresult.size(); i++) {
//
//                    ContractShowDTO fbs = new ContractShowDTO();
//
//                    fbs.setContract_id(seresult.get(i)[1]);
//                    fbs.setContract_name(seresult.get(i)[2]);
//                    fbs.setContract_code(seresult.get(i)[3]);
//                    fbs.setContract_customer(seresult.get(i)[4]);
//                    fbs.setContract_phone(seresult.get(i)[5]);
//                    fbs.setOrganization_id(seresult.get(i)[6]);
//                    fbs.setMember_id(seresult.get(i)[7]);
//                    fbs.setContract_date(seresult.get(i)[8]);
//                    fbs.setContract_number(seresult.get(i)[9]);
//                    fbs.setAttachment_id(seresult.get(i)[10]);
//                    fbs.setIsverify(seresult.get(i)[11]);
//                    fbs.setOrganization_name(seresult.get(i)[12]);
//                    fbs.setAttachment_addr(seresult.get(i)[13]);
//
//                    ResultDTO worklist=null;
//
//                    if(json.has("isverify")){
//                        String state=json.getString("isverify");
//                        worklist=workFlowResource.getWorkFlowInfo("{work_flow_formid:"+seresult.get(i)[1]+",work_flow_state:"+state+"}","1","20");
//                    }else{
//                        worklist=workFlowResource.getWorkFlowInfo("{work_flow_formid:"+seresult.get(i)[1]+"}","1","20");
//                    }
//
//                    fbs.setWs(worklist.getValues());
//
//                    result.add(fbs);
//                }
//            }
//            if (seresult.size() != 0) {
//                resultDTO.setStatus("90000");
//                resultDTO.setDescritpion("报销申请调用成功");
//            } else {
//                resultDTO.setStatus("90001");
//                resultDTO.setDescritpion("报销申请调用失败");
//            }
//            resultDTO.setValues(result);
//
//        } catch (JSONException e) {
//            e.printStackTrace();
//
//        }
        return null;
    }

    /**
     * 增加合同
     */

    public ResultDTO addContractInfo(ContractPullDTO contractPullDTO) {

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = df.format(new Date());

        int resultls = 0;

        String existName = "";
        /**
         * 判断合同申请单中是否已存在此数据
         *
         * 若存在  进行更新
         *
         * 如不存在 执行add方法
         *
         */
        String contract_id = contractPullDTO.getContract_id();
        if (contract_id != null && !contract_id.equals("")) {

            String sql = "select count(1) from contract where contract_id='" + contractPullDTO.getContract_id() + "'";

            existName = getReportingToOne(sql);

            if (!existName.equals("0")) {
                return updateContractInfo(contractPullDTO);
            }
        }

        //构建返回对象
        ResultDTO resultDTO = new ResultDTO();
        String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
        // if(existCarmanage.equals("0")){
        String attach_id = "";
        if (null != contractPullDTO.getFilestream() && !"".equals(contractPullDTO.getFilestream())) {
            attach_id = getImage(contractPullDTO);
        }


        String contract_insert = "";
        contract_insert += "insert into contract (contract_id,contract_firstmember,contract_secondmember," +
                "contract_code,contract_type,contract_signdate,contract_takedate,contract_enddate,contract_desc," +
                "member_id,attachment_id,isverify,fail_resource,contract_updatetime,create_time) " +
                "values ('" + uuid + "','" + contractPullDTO.getContract_firstmember() + "','" + contractPullDTO.getContract_secondmember() + "','" + contractPullDTO.getContract_code() + "'" +
                ",'" + contractPullDTO.getContract_type() + "','" + contractPullDTO.getContract_signdate() + "','" + contractPullDTO.getContract_takedate() + "','" + contractPullDTO.getContract_enddate() + "','" + contractPullDTO.getContract_desc() + "'," +
                "'" + contractPullDTO.getMember_id() + "','" + attach_id + "','" + 0 + "','" + " " + "','" + " " + "','" + df.format(new Date()) + "')";
        //插入业务表
        resultls = addReportingTo(contract_insert);
        // }

        if (resultls != 0) {
            resultDTO.setStr(uuid);
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.add_success);

            if (null != contractPullDTO.getContractwarnperple() && !"".equals(contractPullDTO.getContractwarnperple())) {
                String contracttype = contractPullDTO.getContractwarnperple();
                try {
                    JSONArray array = new JSONArray(contracttype);
                    if (array != null && array.length() > 0) {
                        List<ContractPullDTO> result = new ArrayList<ContractPullDTO>();
                        for (int i = 0; i < array.length(); i++) {
                            JSONObject jsonObject = array.getJSONObject(i);
                            String id = jsonObject.getString("id");
                            String member_name = jsonObject.getString("name");
                            ContractTypePullDTO contractTypePullDTO1 = new ContractTypePullDTO();
                            contractTypePullDTO1.setContract_id(uuid);
                            contractTypePullDTO1.setMember_id(id);
                            contractTypePullDTO1.setMember_name(member_name);
                            contractTypeResource.addContractTypeInfo(contractTypePullDTO1);
                        }
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
            //操作日志添加
            OperatorLogPullDTO operatorlogPullDTO = new OperatorLogPullDTO();
            operatorlogPullDTO.setWork_form_id(uuid);
            operatorlogPullDTO.setOperator_log_type("合同申请");
            operatorlogPullDTO.setOperator_log_title(contractPullDTO.getContract_code());
            operatorlogPullDTO.setOperator_log_content(contractPullDTO.getContract_desc());
            operatorlogPullDTO.setMember_id(contractPullDTO.getMember_id());
            operatorLogResource.addOperatorLogInfo(operatorlogPullDTO);
            //流程上报
            WorkFlowPullDTO workFlowPullDTO = new WorkFlowPullDTO();
            workFlowPullDTO.setWork_flow_formid(uuid);
            workFlowPullDTO.setWork_flow_type("contract");
            workFlowPullDTO.setWork_flow_desc("合同申请请求审批");
            workFlowPullDTO.setWork_flow_date(time);
            workFlowPullDTO.setWork_flow_order("0");
            workFlowPullDTO.setWork_flow_createid(contractPullDTO.getMember_id());
            workFlowPullDTO.setWork_flow_approval(contractPullDTO.getReport_people());
            workFlowPullDTO.setWork_flow_approval_organization_id(contractPullDTO.getWork_flow_approval_organization_id());
            workFlowPullDTO.setFlow_path_id(contractPullDTO.getFlow_path_id());
            workFlowPullDTO.setWork_flow_suggestion("");
            workFlowResource.addWorkFlowInfo(workFlowPullDTO);

            //合同提醒人员

            ResultDTO contractwarnperplelist = null;
            contract_id = contractPullDTO.getContract_id();
            contractwarnperplelist = contractTypeResource.getContractTypeInfo("{contract_id:"+contract_id+"}","1","20");
            ArrayList<ContractTypeShowDTO> mens = (ArrayList<ContractTypeShowDTO>)contractwarnperplelist.getValues();
            for(int i = 0;i<mens.size();i++){
                ContractTypeShowDTO contractTypeShowDTO= mens.get(i);
                /**
                 * 事物提醒
                 */
                RemindPullDTO remindPullDTO = new RemindPullDTO();
                remindPullDTO.setRemind_content(contractPullDTO.getContract_desc());
                remindPullDTO.setRemind_time(contractPullDTO.getContract_takedate());
                remindPullDTO.setRemind_type("8");
                remindPullDTO.setRemind_form_id(uuid);
                remindPullDTO.setRemind_order("0");
                remindPullDTO.setCreate_time(time);
                remindPullDTO.setRemind_endtime(contractPullDTO.getContract_enddate());
                remindPullDTO.setRemind_member_id(contractTypeShowDTO.getMember_id());
                remindPullDTO.setRemind_starttime(contractPullDTO.getContract_enddate());
                remindPullDTO.setRemind_descritpion("您有一条合同过期，请注意查看！");
                remindResource.addRemindInfo(remindPullDTO);
            }
    } else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.add_fail);
        }

        return resultDTO;

    }

    /**
     * 更新合同
     *
     * @param contractPullDTO
     * @return
     */
    public ResultDTO updateContractInfo(ContractPullDTO contractPullDTO) {

        /**
         * 先删除提醒人员
         */

        ResultDTO resultss = null;
        String contract_id = contractPullDTO.getContract_id();
        ContractTypePullDTO contractTypePullDTO = new ContractTypePullDTO();
        contractTypePullDTO.setContract_id(contract_id);
        contractTypeResource.deleteContractTypeInfo(contractTypePullDTO);
        //  if(resultss!=null){
        if (null != contractPullDTO.getContractwarnperple() && !"".equals(contractPullDTO.getContractwarnperple())) {
            String contracttype = contractPullDTO.getContractwarnperple();
            try {
                JSONArray array = new JSONArray(contracttype);
                if (array != null && array.length() > 0) {
                    List<ContractPullDTO> result = new ArrayList<ContractPullDTO>();
                    for (int i = 0; i < array.length(); i++) {
                        JSONObject jsonObject = array.getJSONObject(i);
                        String id = jsonObject.getString("id");
                        String member_name = jsonObject.getString("name");
                        ContractTypePullDTO contractTypePullDTO1 = new ContractTypePullDTO();
                        contractTypePullDTO1.setContract_id(contractPullDTO.getContract_id());
                        contractTypePullDTO1.setMember_id(id);
                        contractTypePullDTO1.setMember_name(member_name);
                        contractTypeResource.addContractTypeInfo(contractTypePullDTO1);
                    }
                }

            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        ResultDTO resultDTO = new ResultDTO();

        String QUERY = "";

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = df.format(new Date());

        //更新sql语句
        QUERY += "update contract set create_time = '" + time + "'";
        if (null != contractPullDTO.getContract_firstmember() && !"".equals(contractPullDTO.getContract_firstmember())) {
            QUERY += ",contract_firstmember = '" + contractPullDTO.getContract_firstmember() + "'";
        }

        if (null != contractPullDTO.getContract_secondmember() && !"".equals(contractPullDTO.getContract_secondmember())) {
            QUERY += ",contract_secondmember ='" + contractPullDTO.getContract_secondmember() + "'";
        }

        if (null != contractPullDTO.getContract_code() && !"".equals(contractPullDTO.getContract_code())) {
            QUERY += ",contract_code ='" + contractPullDTO.getContract_code() + "'";
        }
        if (null != contractPullDTO.getIsverify() && !"".equals(contractPullDTO.getIsverify())) {
            QUERY += ",isverify ='" + contractPullDTO.getIsverify() + "'";
        }

        if (null != contractPullDTO.getContract_type() && !"".equals(contractPullDTO.getContract_type())) {
            QUERY += ",contract_type ='" + contractPullDTO.getContract_type() + "'";
        }

        if (null != contractPullDTO.getContract_signdate() && !"".equals(contractPullDTO.getContract_signdate())) {
            QUERY += ",contract_signdate ='" + contractPullDTO.getContract_signdate() + "'";
        }

        if (null != contractPullDTO.getContract_takedate() && !"".equals(contractPullDTO.getContract_takedate())) {
            QUERY += ",contract_takedate ='" + contractPullDTO.getContract_takedate() + "'";
        }
        if (null != contractPullDTO.getContract_enddate() && !"".equals(contractPullDTO.getContract_enddate())) {
            QUERY += ",contract_enddate = '" + contractPullDTO.getContract_enddate() + "'";
        }

        if (null != contractPullDTO.getContract_desc() && !"".equals(contractPullDTO.getContract_desc())) {
            QUERY += ",contract_desc ='" + contractPullDTO.getContract_desc() + "'";
        }


        if (null != contractPullDTO.getFail_resource() && !"".equals(contractPullDTO.getFail_resource())) {
            QUERY += ",fail_resource ='" + contractPullDTO.getFail_resource() + "'";
        }
        if (null != contractPullDTO.getAttachment_id() && !"".equals(contractPullDTO.getAttachment_id()) && null != contractPullDTO.getFilestream() && !"".equals(contractPullDTO.getFilestream())) {
            //调用修改文件方法
            updateImage(contractPullDTO);
        } else if (null != contractPullDTO.getFilestream() && !"".equals(contractPullDTO.getFilestream())) {
            String uid = getImage(contractPullDTO);
            QUERY += ",attachment_id='" + uid + "'";

        }

        QUERY += " where contract_id = '" + contractPullDTO.getContract_id() + "'";


        int result = this.addReportingTo(QUERY);

        // 判断SQl是否执行成功
        if (result != 0) {

            WorkFlowPullDTO workFlowPullDTO = new WorkFlowPullDTO();
            workFlowPullDTO.setWork_flow_formid(contractPullDTO.getContract_id());
            workFlowPullDTO.setWork_flow_approval(contractPullDTO.getReport_people());
            workFlowPullDTO.setWork_flow_approval_organization_id(contractPullDTO.getWork_flow_approval_organization_id());
            workFlowPullDTO.setIsverify("0");
            workFlowResource.updateWorkFlowInfo(workFlowPullDTO);
            resultDTO.setStr(contractPullDTO.getContract_id());
            resultDTO.setStatus("90000");//
            resultDTO.setDescritpion(warnSrc.update_success);
        } else {
            resultDTO.setStatus("90001");//
            resultDTO.setDescritpion(warnSrc.update_fail);
        }
        return resultDTO;
    }

    //删除合同
    public ResultDTO deleteContractInfo(ContractPullDTO contractPullDTO) {

        //删除之前查询该表是否有记录
        String sql = "select count(1) from contract where contract_id='" + contractPullDTO.getContract_id() + "'";
        String existName = getReportingToOne(sql);

        int result = 0;

        ResultDTO resultDTO = new ResultDTO();
        if (!existName.equals("0")) {

            String QUERY = "";

            QUERY += "delete from contract where contract_id='" + contractPullDTO.getContract_id() + "'";
            result = this.addReportingTo(QUERY);
        }
        if (result != 0) {
            String delete_sql = "delete from attachment  where attachment_id='" + contractPullDTO.getAttachment_id() + "'";
            this.addReportingTo(delete_sql);
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.delete_success);
            ContractTypePullDTO contractTypePullDTO=new ContractTypePullDTO();
            contractTypePullDTO.setContract_id(contractPullDTO.getContract_id());
            contractTypeResource.deleteContractTypeInfo(contractTypePullDTO);

            WorkFlowPullDTO workFlowPullDTO = new WorkFlowPullDTO();
            workFlowPullDTO.setWork_flow_formid(contractPullDTO.getContract_id());
            workFlowResource.deleteWorkFlowInfo(workFlowPullDTO);
        } else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.delete_fail);
        }

        return resultDTO;
    }

    /**
     * 添加文件信息
     *
     * @param contractPullDTO
     * @return
     */
    public String getImage(ContractPullDTO contractPullDTO) {
        ResultDTO resultDTO = new ResultDTO();
        // TODO Auto-generated method stub
        String enpenform_image = "";
        //获取配置图片地址
        String[] image_path = this.getImage_path();
        String image_pathAddr = image_path[1];//服务器项目目录地址
        String image_pathSerAddr = image_path[2];//图片访问地址
        String imgDirct = image_pathAddr + "/contract";
        File f = new File(imgDirct);
        if (!f.exists()) {
            f.mkdirs();
            f.setWritable(true, false);
        }
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");

        String imgPath = imgDirct + "/" + contractPullDTO.getFile_type();


        //base64字符串保存为图片
        boolean headImg = FileProcess.GenerateImage(contractPullDTO.getFilestream(), imgPath);
        FileProcess.newlog("执行头像图片储存方法，储存照片地址：" + imgPath + " 方法返回：" + headImg);
        int resultls = 0;
        int mresult = 0;

        String imgurl = "/contract/" + contractPullDTO.getFile_type();

        //ArrayList<HeadPortaitDTO> result = new ArrayList<HeadPortaitDTO>();

        if (headImg) {
            //插入头像表
            enpenform_image += "insert into attachment(attachment_id,attachment_addr,head_portrait_time,file_type,file_name) values ('" + uuid + "','" + imgurl + "','" + df.format(new Date()) + "','" + contractPullDTO.getFile_type() + "','" + contractPullDTO.getFile_name() + "')";
            // enpenform_image += "insert into file(file_id,file_addr,file_time,filegui_id,file_name) values ('" + uuid + "','" +imgurl+ "','" +df.format(new Date()) + "','" + dataDTO.getFilegui_id() + "','" + dataDTO.getFiletype() + "')";
            resultls = addReportingTo(enpenform_image);
            if (resultls != 0) {
                return uuid;
            }
        }
        //执行sql
        return null;
    }

//    public String getImage(String image) {
//        // TODO Auto-generated method stub
//        String enpenform_image = "";
//        //获取配置附件地址
//        String[] image_path = this.getImage_path();
//        String image_pathAddr = image_path[1];//服务器项目目录地址
//        String image_pathSerAddr = image_path[2];//图片访问地址
//        String imgDirct = image_pathAddr + "/contract";
//        File f = new File(imgDirct);
//        if (!f.exists()) {
//            f.mkdirs();
//            f.setWritable(true, false);
//        }
//        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
//
//        String imgPath = imgDirct + "/" + uuid + ".jpg";
//
//        //base64字符串保存为图片
//        boolean headImg = FileProcess.GenerateImage(image, imgPath);
//        FileProcess.newlog("执行头像图片储存方法，储存照片地址：" + imgPath + " 方法返回：" + headImg);
//        int resultls = 0;
//        int mresult = 0;
//
//        String imgurl = image_pathSerAddr + "/contract/" + uuid + ".jpg";
//
//        //ArrayList<HeadPortaitDTO> result = new ArrayList<HeadPortaitDTO>();
//
//        if (headImg) {
//            //插入头像表
//            enpenform_image += "insert into attachment(attachment_id,attachment_addr,head_portrait_time,head_portrait_type) values ('" + uuid + "','" + imgurl + "','" + df.format(new Date()) + "','"+11+"')";
//        }
//
//        //执行sql
//        resultls = addReportingTo(enpenform_image);
//
//        if (resultls != 0) {
//            return uuid;
//        }
//        return null;
//    }

    /**
     * 修改图片方法
     *
     * @param
     * @return
     */
    public String updateImage(ContractPullDTO contractPullDTO) {
        // TODO Auto-generated method stub
        String enpenform_image = "";
        //获取配置图片地址
        String[] image_path = this.getImage_path();
        String image_pathAddr = image_path[1];//服务器项目目录地址
        String image_pathSerAddr = image_path[2];//图片访问地址
        String imgDirct = image_pathAddr + "/contract";
        File f = new File(imgDirct);
        if (!f.exists()) {
            f.mkdirs();
            f.setWritable(true, false);
        }
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");

        String imgPath = imgDirct + "/" + contractPullDTO.getFile_type();

        //base64字符串保存为图片
        boolean headImg = FileProcess.GenerateImage(contractPullDTO.getFilestream(), imgPath);
        FileProcess.newlog("执行头像图片储存方法，储存照片地址：" + imgPath + " 方法返回：" + headImg);
        int resultls = 0;
        int mresult = 0;

        String imgurl = "/contract/" + contractPullDTO.getFile_type();

        //ArrayList<HeadPortaitDTO> result = new ArrayList<HeadPortaitDTO>();

        if (headImg) {
            //修改头像表
            enpenform_image += "update attachment set attachment_addr='" + imgurl + "',head_portrait_time = '" + df.format(new Date()) + "',file_type = '" + contractPullDTO.getFile_type() + "',file_name = '" + contractPullDTO.getFile_name() + "' where attachment_id='" + contractPullDTO.getAttachment_id() + "'";
            // enpenform_image += "insert into attachment(attachment_id,attachment_addr,head_portrait_time) values ('" + uuid + "','" + imgurl + "','" + df.format(new Date()) + "')";
            //执行sql
            resultls = addReportingTo(enpenform_image);

        }
        if (resultls != 0) {
            return uuid;
        }

        return null;
    }
}
